import request from "../util/request.ts";
import {Permission, Role} from "./type/model.ts";

/**
 * 分页查询角色列表
 * @param currentPage 当前页码
 * @param pageSize 每页大小
 * @param role 查询条件
 * @param during
 * @returns 角色列表及分页信息
 */
export const fetchRolePage = async (currentPage: number, pageSize: number, role: Role, during:[]): Promise<any> => {
    return await request.get(`/role/page`, {
        params: {
            currentPage,
            pageSize,
            role: JSON.stringify(role),
            during: during?during.join(','):[]
        }
    });
}

/**
 * 添加角色
 * @param role 角色对象
 * @param permissionIds
 * @returns 添加结果
 */
export const postRole = async (role: Role, permissionIds: Number[]): Promise<any> => await request.post("/role", {
    role,
    permissionIds
});

/**
 * 更新角色
 * @param role 角色对象
 * @returns 更新结果
 */
export const putRole = async (role: Role): Promise<any> => await request.put("/role", role);

/**
 * 删除角色
 * @param roleId 角色 ID
 * @returns 删除结果
 */
export const delRole = async (roleId: number): Promise<any> => await request.delete(`/role/${roleId}`);

/**
 * 批量删除角色
 * @param roleIds 角色 ID 数组
 * @returns 删除结果
 */
export const batchDelRole = async (roleIds: number[]): Promise<any> => await request.delete(`/role/batch/delete`, {
    params: {
        roleIds: roleIds.join(',')
    }
});

/**
 * 导出角色数据
 * @returns 导出的文件
 */
export const exportRoleData = async (): Promise<any> => await request.get("/role/export", { responseType: "blob" });

/**
 * 获取角色详情
 * @param roleId 角色 ID
 * @returns 角色详情
 */
export const fetchRoleById = async (roleId: number): Promise<any> => await request.get(`/role/${roleId}`);


export const fetchRolePermission = async (roleId: number): Promise<any> => await request.get(`/role/permission/${roleId}`);

export const putRolePermission = async (roleId: number, permissionIds: number[]): Promise<any> => await request.put(`/role/permission/${roleId}`,  permissionIds);